// packageH/project_verification/WriteOffRecord_evaluate/WriteOffRecord_evaluate.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    answer: [],
    chhild: [],
    project: [],
    store_name: "",
    islook: false,
    isclick: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.look) {
      this.setData({
        islook: true
      })
      this.recordList();
    } else {
      this.getData();
    }
  },
  async recordList() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-projects.frontend.comment.detail"),
      data: {
        log_id: this.options.id
      },
    })
    if (!data.result) {
      app.tips(data.msg)
      return
    }
    this.setData({
      project: {
        project_name: data.data.project_name,
        project_thumb: data.data.project_thumb
      },
      store_name: data.data.store_name,
      info: data.data.data,
    })
    this.data.info.forEach((ele, ind) => {
      this.data.chhild[ind] = ele.select_value;
    })
    this.setData({
      chhild: this.data.chhild
    })
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-projects.frontend.comment.index"),
      data: {
        log_id: this.options.id
      },
    })
    if (!data.result) {
      app.tips(data.msg)
      return
    } else {
      this.setData({
        info: data.data.question_list,
        project: data.data.project,
        store_name: data.data.store_name,
      })
      this.data.info.forEach((ele, ind) => {
        this.data.answer[ind] = {};
        this.data.answer[ind].id = ele.id;
        this.data.answer[ind].options = "";
      })
      this.setData({
        answer: this.data.answer
      })
      console.log(this.data.answer)
    }
  },
  changeGroup(e) {
    let index = e.currentTarget.dataset.index;
    this.data.answer[index].options = e.detail
  },
  async confirm() {
    if (this.data.isclick) return
    this.data.isclick = true;
    setTimeout(() => {
      this.data.isclick = false;
    }, 1000);
    let answer = this.data.answer;
    let obj = {};
    answer.forEach((ele, ind) => {
      obj[ele.id] = ele.options;
    });
    // 提交评价必填验证
    for (let i = 0; i < this.data.info.length; i++) {
      if (!obj[this.data.info[i].id]) {
        app.tips(`请勾选第${i + 1}题评价`)
        break;
      }
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-projects.frontend.comment.submit"),
      data: {
        log_id: this.options.id,
        question_list: obj
      },
    })
    if (!data.result) {
      app.tips(data.msg)
      return
    } else {
      this.setData({
        islook: true
      })
      this.recordList();
      app.tips(data.msg)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})